package com.ep.demo.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ep.autoload.TestAuload;
import com.ep.demo.entity.Book;
import com.ep.demo.service.IBookService;
import com.ep.entity.Paper;
import com.ep.util.IBaseSearch;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
@Controller("bookAction")
@Scope("prototype")
public class BookAction implements ModelDriven<Book> {
	@Resource(name="bookService")
	private IBookService bookService;
	public String show(){
		System.out.println("show----------------------");
//		bookService.saveBook(new Book());
		return "show";
	}
	public String add(){
		System.out.println("book add....");
		return "add";
	}
	public String save(){
		System.out.println("save....");
		bookService.saveBook(book);
		return "show";
	}
	public String search(){
//		ISearchUtils searchUtils = (ISearchUtils) new BookProxy().bind(new SearchUtils());
//		System.out.println("search start.....");
//		SearchUtils searchUtils = new SearchUtils();
//		String html = searchUtils
//				.get("http://item.yhd.com/item/53944722?tc=3.0.5.53944722.7&tp=52.23586.108.7.3.LBsksZ1-10-EVNUx&ti=2M4W");
//		System.out.println("---------------------------");
//		searchUtils.search(html,"");
		IBaseSearch ibs=null;
		String searchWeb="CNKI";
		Map<String,String> classMap=new HashMap<String,String>();
		classMap.put("CNKI", "com.ep.util.CNKISearch");
		if(StringUtils.isNotBlank(classMap.get(searchWeb))){
			ibs=(IBaseSearch) com.ep.factory.BeanFactory.newInstanceBean(classMap.get(searchWeb));
		}
		List<Paper> papers=null;
		if(null!=ibs){
			papers = ibs.search();
		}
		bookService.savePaperList(papers);
		return "show";
	}
	public String testAuto(){
		new TestAuload().autoLoad("com.ep.util.CNKISearch");
		return "show";
	}
	/**
	 * Ajax 方法1,利用servlet 原生的方法 
	 */
	public void strutsAjax(){
		String autoJson="[{\"a\":\"b\",\"c\":\"d\"},{\"a\":\"b\",\"c\":\"d\"}]";
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.getWriter().write(autoJson);
			response.flushBuffer();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	/**
	 *Ajax 方法2,利用struts 封装的方法 
	 *会自动封装实体和list
	 */
	public String strutsAjax2(){
//		String autoJson="[{\"a\":\"b\",\"c\":\"d\"},{\"a\":\"b\",\"c\":\"d\"}]";
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> request = ac.getContextMap();
//		request.put("json", autoJson);
//		json=autoJson;
//		System.out.println(autoJson);
		List<Book> list=new ArrayList<Book>();
		for(int i=0;i<5;i++){
			Book book=new Book();
			book.setId(i+"");
			book.setName("json:"+i);
			book.setType("type:"+(i&8));
			book.setUnitId(i+"");
			list.add(book);
		}
		ac.getValueStack().push(list);
		return "json";
	}
	/**
	 * 文件上传  使用流的方式保存文件
	 */
	public String savePhoto(){
		try {
			String path = ServletActionContext.getServletContext().getRealPath(
					"/attachment/");
			HttpServletRequest request = ServletActionContext.getRequest();
			InputStream in = new FileInputStream(photo);
			//乱码问题不知道怎么解决
			File file = new File(path + "/"
					+ new String(photoFileName.getBytes("ISO8859-1"),"UTF-8"));
			OutputStream ops=new FileOutputStream(file);
			int len=0;
			byte[] b=new byte[1024];
			while((len=in.read(b))!=-1){
				ops.write(b, 0, len);
			}
			ops.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "show";
	}
	/**
	 *文件下载方法1,servlet 流的方式 
	 */
	public void downLoad1(){
		try {
//			this.getClass().getResourceAsStream("");
			String path = ServletActionContext.getServletContext().getRealPath("/download/");
			File file = new File(path+"/001.jpg");
			InputStream in=new FileInputStream(file);
			HttpServletResponse response = ServletActionContext.getResponse();
			ServletOutputStream out = response.getOutputStream();
			//设置文件下载类型
			response.setContentType("image/jpeg");
		    response.setHeader("Content-disposition", "attachment; filename="
		              + "001" + ".jpg");
			int len=0;
			byte[] b=new byte[1024];
			while((len=in.read(b))!=-1){
				out.write(b, 0, len);
			}
			out.flush();
			out.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 文件下载方法2,struts2 自带方法
	 */
	public String downLoad2(){
		try {
			String path = ServletActionContext.getServletContext().getRealPath(
					"/download/");
			File file = new File(path + "/001.jpg");
			photoDown = new FileInputStream(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "download";
	}
	public IBookService getBookService() {
		return bookService;
	}
	public void setBookService(IBookService bookService) {
		this.bookService = bookService;
	}
	private String json="";
	private Book book=new Book();
	private File photo=null;
	private String photoFileName;
	private String photoContentType;
	public Book getModel() {
		return book;
	}
	private InputStream photoDown;
	
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public File getPhoto() {
		return photo;
	}
	public void setPhoto(File photo) {
		this.photo = photo;
	}
	public String getPhotoFileName() {
		return photoFileName;
	}
	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}
	public String getPhotoContentType() {
		return photoContentType;
	}
	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}
	public InputStream getPhotoDown() {
		return photoDown;
	}
	public void setPhotoDown(InputStream photoDown) {
		this.photoDown = photoDown;
	}
}
